﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common;
using SYH.Config;
using System.Data.SqlClient;

namespace SYH.Public.DAL
{
    public partial class Public_AdInfo
    {
        public Public_AdInfo()
        { }
        #region  基本方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Public_AdInfo");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH.Public.Model.Public_AdInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Public_AdInfo(");
            strSql.Append("Title,AdAreaId,StartTime,EndTime,AdLink,AdImage,AdContent,Status,Sort,CreateTime)");
            strSql.Append(" values (");
            strSql.Append("@Title,@AdAreaId,@StartTime,@EndTime,@AdLink,@AdImage,@AdContent,@Status,@Sort,@CreateTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,20),
					new SqlParameter("@AdAreaId", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@AdLink", SqlDbType.VarChar,50),
					new SqlParameter("@AdImage", SqlDbType.VarChar,100),
					new SqlParameter("@AdContent", SqlDbType.NVarChar,300),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.AdAreaId;
            parameters[2].Value = model.StartTime;
            parameters[3].Value = model.EndTime;
            parameters[4].Value = model.AdLink;
            parameters[5].Value = model.AdImage;
            parameters[6].Value = model.AdContent;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Sort;
            parameters[9].Value = model.CreateTime;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows ;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH.Public.Model.Public_AdInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Public_AdInfo set ");
            strSql.Append("Title=@Title,");
            strSql.Append("AdAreaId=@AdAreaId,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("AdLink=@AdLink,");
            strSql.Append("AdImage=@AdImage,");
            strSql.Append("AdContent=@AdContent,");
            strSql.Append("Status=@Status,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("CreateTime=@CreateTime");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,20),
					new SqlParameter("@AdAreaId", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@AdLink", SqlDbType.VarChar,50),
					new SqlParameter("@AdImage", SqlDbType.VarChar,100),
					new SqlParameter("@AdContent", SqlDbType.NVarChar,300),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.AdAreaId;
            parameters[2].Value = model.StartTime;
            parameters[3].Value = model.EndTime;
            parameters[4].Value = model.AdLink;
            parameters[5].Value = model.AdImage;
            parameters[6].Value = model.AdContent;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Sort;
            parameters[9].Value = model.CreateTime;
            parameters[10].Value = model.Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Public_AdInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Public_AdInfo ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return rows > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.Public.Model.Public_AdInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,AdAreaId,StartTime,EndTime,AdLink,AdImage,AdContent,Status,Sort,CreateTime from Public_AdInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH.Public.Model.Public_AdInfo model = new SYH.Public.Model.Public_AdInfo();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.Public.Model.Public_AdInfo DataRowToModel(DataRow row)
        {
            SYH.Public.Model.Public_AdInfo model = new SYH.Public.Model.Public_AdInfo();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["AdAreaId"] != null && row["AdAreaId"].ToString() != "")
                {
                    model.AdAreaId = int.Parse(row["AdAreaId"].ToString());
                }
                if (row["StartTime"] != null && row["StartTime"].ToString() != "")
                {
                    model.StartTime = DateTime.Parse(row["StartTime"].ToString());
                }
                if (row["EndTime"] != null && row["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(row["EndTime"].ToString());
                }
                if (row["AdLink"] != null)
                {
                    model.AdLink = row["AdLink"].ToString();
                }
                if (row["AdImage"] != null)
                {
                    model.AdImage = row["AdImage"].ToString();
                }
                if (row["AdContent"] != null)
                {
                    model.AdContent = row["AdContent"].ToString();
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Title,AdAreaId,StartTime,EndTime,AdLink,AdImage,AdContent,Status,Sort,CreateTime ");
            strSql.Append(" FROM Public_AdInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Title,AdAreaId,StartTime,EndTime,AdLink,AdImage,AdContent,Status,Sort,CreateTime ");
            strSql.Append(" FROM Public_AdInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder + " desc");
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Public_AdInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return Common.TypeParse.ObjectToInt(obj, 0);
        }
        #endregion

        #region 更新一个字段
        /// <summary>
        /// 更新一个字段
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="value">值</param>
        /// <param name="strWhere">条件</param>
        /// <param name="flag">字符串表示：true为字符串；false为整型、bool、日期等</param>
        /// <returns></returns>
        public bool UpdateOneField(string name, string value, string strWhere, bool flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE Public_AdInfo set ");
            strSql.Append(" " + name + "= ");
            if (flag)
            {
                strSql.Append(" '" + value + "' ");
            }
            else
            {
                strSql.Append(" " + value + " ");
            }
            strSql.Append(" WHERE " + strWhere);

            return SqlHelper.ExecuteSql(Config.WebConfig.SqlConnString, strSql.ToString()) > 0;
        }
        #endregion

        #region 广告信息分页数据
        /// <summary>
        /// 广告信息分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strCondition"></param>
        /// <param name="pageCount"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageData(int pageSize, int pageIndex, string strCondition, out int pageCount, out int recordCount)
        {
            DataTable dt = Common.PublicSqlData.SimplePager(WebConfig.SqlConnString,
                                                            "Public_AdInfo as pi inner join Public_AdAreaInfo as pa on pa.Id=pi.AdAreaId",
                                                            " pi.Id,pi.Title as AdInfoName,pa.Title as AdAreaName,pi.AdAreaId,pi.AdLink,pi.AdImage,pi.StartTime,pi.EndTime,pi.Sort",
                                                            pageSize,
                                                            pageIndex,
                                                            "Id",
                                                            true,
                                                            strCondition,
                                                            out pageCount,
                                                            out recordCount).Tables[0];

            return dt;
        }
        #endregion
    }
}
